<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Mm_Atom_Element_Feed extends Mm_Xml_Element_Parent {
    
    const ELEMENT_ID      = 'id';
    const ELEMENT_TITLE   = 'title';
    const ELEMENT_UPDATED = 'updated';
    const ELEMENT_AUTHOR  = 'author';
    
    const ELEMENT_ACTIVITY_SUBJECT = 'activity:subject';
    const ELEMENT_GENERATOR = 'generator';
    const ELEMENT_SUBTITLE  = 'subtitle';
    const ELEMENT_LOGO      = 'logo';
    const ELEMENT_ENTRIES   = 'entries';
    
    const ELEMENT_LINK_HUB            = 'hub';
    const ELEMENT_LINK_SALMON         = 'salmon';
    const ELEMENT_LINK_SALMON_REPLIES = 'http://salmon-protocol.org/ns/salmon-replies';
    const ELEMENT_LINK_SALMON_MENTION = 'http://salmon-protocol.org/ns/salmon-mention';
    const ELEMENT_LINK_ALTERNATE      = "alternate";
    
    const SORT_BY_UPDATED_TIMESTAMP = true;
    const DO_NOT_SORT_BY_UPDATED_TIMESTAMP = false;
    
    protected $tagname = "feed";
    
    public function __construct(array $childElements, array $namespaces) {
        parent::__construct($childElements);
        
        $this->setNamespaces($namespaces);
        
// Not sure yet if we need this.        
//        if($this->getAuthor() === null) {
//            $entries = $this->getEntriesAsArray();
//            $this->addChildElement(self::ELEMENT_AUTHOR,
//                                   $entries[0]->getChildElement(Mm_Atom_Element_Entry::ELEMENT_AUTHOR));
//        }
    }
    
    /**
     * @return Mm_Atom_Elemen_Generator
     */
    public function getGenerator() {
        return $this->getChildElement(self::ELEMENT_GENERATOR);
    }
    
    /**
     * @return Mm_Atom_Elemen_Id
     */
    public function getId() {
        return $this->getChildElement(self::ELEMENT_ID);
    }
    
    /**
     * @return Mm_Atom_Elemen_Title
     */
    public function getTitle() {
        return $this->getChildElement(self::ELEMENT_TITLE);
    }
    
    /**
     * @return Mm_Atom_Elemen_Updated
     */
    public function getUpdated() {
        return $this->getChildElement(self::ELEMENT_UPDATED);
    }

    /**
     * @return Mm_Atom_Element_Author
     */
    public function getAuthor() {
        return $this->getChildElement(self::ELEMENT_AUTHOR);
    }
    
	/**
     * @return Mm_Atom_Element_Link
     */
    public function getHubLink() {
        return $this->getChildElement(self::ELEMENT_LINK_HUB);
    }
    
	/**
     * @return Mm_Atom_Element_Link
     */
    public function getSalmonLink() {
        return $this->getChildElement(self::ELEMENT_LINK_SALMON);
    }
    
	/**
     * @return Mm_Atom_Element_Link
     */
    public function getSalmonMentionLink() {
        return $this->getChildElement(self::ELEMENT_LINK_SALMON_MENTION);
    }
    
	/**
     * @return Mm_Atom_Element_Link
     */
    public function getSalmonRepliesLink() {
        return $this->getChildElement(self::ELEMENT_LINK_SALMON_REPLIES);
    }
    
	/**
     * @return Mm_Atom_Element_Link
     */
    public function getAlternateLink() {
        return $this->getChildElement(self::ELEMENT_LINK_ALTERNATE);
    }
    
	/**
     * @return Mm_Atom_Element_ActivityStream_Subject
     */
    public function getActivitySubject() {
        return $this->getChildElement(self::ELEMENT_ACTIVITY_SUBJECT);
    }
    
    public function getEntriesAsArray() {
        return $this->getChildElement(self::ELEMENT_ENTRIES);
    }
    
    public function getEntries($sortByUpdated = self::SORT_BY_UPDATED_TIMESTAMP) {
        $ret = array();
        foreach ($this->getChildElement(self::ELEMENT_ENTRIES) as $entry) {
            $ret[$entry->getId()->value] = $entry;
        }
        
        if($sortByUpdated) {
            uasort($ret, array("Mm_Atom_Element_Feed", "cmp_entries"));
        }
        
        return $ret;
    }
    
    public static function cmp_entries($a, $b) {
        $at = strtotime($a->getUpdated()->value);
        $bt = strtotime($b->getUpdated()->value);
        if ($at == $bt) {
            return 0;
        }
        return ($at < $bt) ? +1 : -1;
    }
}

